2020-2021 Sunseeker Telemetry and Lighting System
adc_ex1_avccRef.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
63 //******************************************************************************
64 #include "driverlib.h"
65 #include "Board.h"
66 
67 void main (void)
68 {
69  //Stop Watchdog Timer
70  WDT_A_hold(WDT_A_BASE);
71 
72  //Set A7 as an input pin.
73  //Set appropriate module function
74  GPIO_setAsPeripheralModuleFunctionInputPin(
75  GPIO_PORT_ADC7,
76  GPIO_PIN_ADC7,
77  GPIO_FUNCTION_ADC7);
78 
79  //Set LED1 as an output pin.
80  GPIO_setAsOutputPin(
81  GPIO_PORT_LED1,
82  GPIO_PIN_LED1);
83 
84  //Set LED2 as an output pin.
85  GPIO_setAsOutputPin(
86  GPIO_PORT_LED2,
87  GPIO_PIN_LED2);
88 
89  /*
90  * Disable the GPIO power-on default high-impedance mode to activate
91  * previously configured port settings
92  */
93  PMM_unlockLPM5();
94 
95  //Initialize the ADC Module
96  /*
97  * Base Address for the ADC Module
98  * Use internal ADC bit as sample/hold signal to start conversion
99  * USE MODOSC 5MHZ Digital Oscillator as clock source
100  * Use default clock divider of 1
101  */
102  ADC_init(ADC_BASE,
103  ADC_SAMPLEHOLDSOURCE_SC,
104  ADC_CLOCKSOURCE_ADCOSC,
105  ADC_CLOCKDIVIDER_1);
106 
107  ADC_enable(ADC_BASE);
108 
109  /*
110  * Base Address for the ADC Module
111  * Sample/hold for 16 clock cycles
112  * Do not enable Multiple Sampling
113  */
114  ADC_setupSamplingTimer(ADC_BASE,
115  ADC_CYCLEHOLD_16_CYCLES,
116  ADC_MULTIPLESAMPLESDISABLE);
117 
118  //Configure Memory Buffer
119  /*
120  * Base Address for the ADC Module
121  * Use input A7
122  * Use positive reference of AVcc
123  * Use negative reference of AVss
124  */
125  ADC_configureMemory(ADC_BASE,
126  ADC_INPUT_A7,
127  ADC_VREFPOS_AVCC,
128  ADC_VREFNEG_AVSS);
129 
130  ADC_clearInterrupt(ADC_BASE,
131  ADC_COMPLETED_INTERRUPT);
132 
133  //Enable Memory Buffer interrupt
134  ADC_enableInterrupt(ADC_BASE,
135  ADC_COMPLETED_INTERRUPT);
136 
137  for (;;)
138  {
139  //Delay between conversions
140  __delay_cycles(5000);
141 
142  //Enable and Start the conversion
143  //in Single-Channel, Single Conversion Mode
144  ADC_startConversion(ADC_BASE,
145  ADC_SINGLECHANNEL);
146 
147  //LPM0, ADC10_ISR will force exit
148  __bis_SR_register(CPUOFF + GIE);
149  //For debug only
150  __no_operation();
151 
152 
153  }
154 }
155 
156 //ADC10 interrupt service routine
157 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
158 #pragma vector=ADC_VECTOR
159 __interrupt
160 #elif defined(__GNUC__)
161 __attribute__((interrupt(ADC_VECTOR)))
162 #endif
163 void ADC_ISR (void)
164 {
165  switch (__even_in_range(ADCIV,12)){
166  case 0: break; //No interrupt
167  case 2: break; //conversion result overflow
168  case 4: break; //conversion time overflow
169  case 6: break; //ADC10HI
170  case 8: break; //ADC10LO
171  case 10: break; //ADC10IN
172  case 12: //ADC10IFG0
173  //(Automatically clears ADC10IFG0 by reading memory buffer)
174  if (ADC_getResults(ADC_BASE) < 0x1FF){
175 
176  //Turn LED1 off
178  GPIO_PORT_LED1,
179  GPIO_PIN_LED1
180  );
181 
182  //Turn LED2 off
184  GPIO_PORT_LED2,
185  GPIO_PIN_LED2
186  );
187  }
188  else {
189  //Turn LED1 on
191  GPIO_PORT_LED1,
192  GPIO_PIN_LED1
193  );
194 
195  //Turn LED2 on
197  GPIO_PORT_LED2,
198  GPIO_PIN_LED2
199  );
200  }
201 
202  //Clear CPUOFF bit from 0(SR)
203  //Breakpoint here and watch ADC_Result
205  break;
206  default: break;
207  }
208 }
void main(void)
void ADC_ISR(void)
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)